import Koa from 'koa';
import KoaCompose from 'koa-compose';
import KoaLogger from 'koa-logger';
import KoaJSON from 'koa-json';
import KoaBody from 'koa-body';

// tips 处理函数
import errorHandle from './handle/error.handle';

// tips 自定义中间件
import context_extendMiddleware from '@/middleware/context_extend.middleware';
import {
  transformQueryToBody,
} from '@/middleware/http.middleware';
import utilsMiddleware from '@/middleware/utils.middleware';
import router from '@/router';

const App = new Koa();

// tips 安装中间件
const setup = KoaCompose([
  KoaLogger(),
  KoaJSON(),
  KoaBody(),
  context_extendMiddleware,
  utilsMiddleware,
  transformQueryToBody,
  ...router,
]);

App.use(setup);

App.on('error', errorHandle);

export default App;
